import axios from '../index'

/**
 * 监控Tab API管理器
 */
class MonitorTabManager {
    constructor() {
        this.baseUrl = '/monitor'
    }

    // 获取端口、设备、变量、mqtt的数量
    async getHardwareCount(deviceId) {
        try {
            const response = await axios.get(`${this.baseUrl}/hardware/count`, { params: { deviceId } })
            console.log('✅ [TabManager] 获取硬件数量成功', { response })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取硬件数量失败:', error)
            throw error
        }
    }
    
    async readVariable(data) {
        console.log('🔍 [TabManager] 读取变量', { data })
        try {
            const response = await axios.get(`${this.baseUrl}/variables/read`, { data })
            console.log('✅ [TabManager] 读取变量成功', { data })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 读取变量失败:', error)
            throw error
        }
    }

    async writeVariable(data) {
        console.log('🔍 [TabManager] 写入变量', { data })
        try {
            const response = await axios.get(`${this.baseUrl}/variables/write`, { data })
            console.log('✅ [TabManager] 写入变量成功', { data })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 写入变量失败:', error)
            throw error
        }
    }

    async getLt645Options(params = {}) {
        console.log('🔍 [TabManager] 获取LT645协议选项数据', { params })
        try {
            const response = await axios.get(`${this.baseUrl}/lt645/options`, { params })
            console.log('✅ [TabManager] 获取LT645协议选项数据成功', { response })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取LT645协议选项数据失败:', error)
            throw error
        }
    }

    async getPortLoadDeviceType(updateMode) {
        console.log('🔍 [TabManager] 获取端口挂载设备类型数据', { updateMode })
        try {
            // 构建查询参数
            const response = await axios.get(`${this.baseUrl}/ports/load-device-type`, { params: { updateMode } })
            console.log('✅ [TabManager] 获取端口挂载设备类型数据成功', { response })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取端口挂载设备类型数据失败:', error)
            throw error
        }
    }

    async getBridgePortOptions() {
        console.log('🔍 [TabManager] 获取桥接端口号选项数据')
        try {
            const response = await axios.get(`${this.baseUrl}/ports/bridge-port-options`)
            console.log('✅ [TabManager] 获取桥接端口号选项数据成功', { response })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取桥接端口号选项数据失败:', error)
            throw error
        }
    }

    async getVarRegistersTypeOptions(portProtoType) {
        console.log('🔍 [TabManager] 获取变量寄存器类型选项数据')
        try {
            // 构建查询参数
            const response = await axios.get(`${this.baseUrl}/variables/registers/type-options`, { params: { portProtoType } })
            console.log('✅ [TabManager] 获取变量寄存器类型选项数据成功', { response })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取变量寄存器类型选项数据失败:', error)
            throw error
        }
    }

    // 编码类型管理
    async getCodeTypeList() {
        console.log('🔍 [TabManager] 获取编码类型列表')
        try {
            const response = await axios.get(`${this.baseUrl}/code-types`)
            console.log('✅ [TabManager] 获取编码类型列表成功', { response })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取编码类型列表失败:', error)
            throw error
        }
    }

    async getCodeTypeData(codeType) {
        console.log('🔍 [TabManager] 获取编码类型数据', { codeType })
        try {
            const response = await axios.get(`${this.baseUrl}/code-types/${codeType}/data`)
            // console.log('✅ [TabManager] 获取编码类型数据成功', { codeType, response })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取编码类型数据失败:', error)
            throw error
        }
    }

    // 端口管理
    async getPortList(groupDeviceId = null, params = {}) {
        console.log('🔍 [TabManager] 获取端口列表', { groupDeviceId, params })
        try {
            // 构建查询参数
            const queryParams = { ...params }
            if (groupDeviceId) {
                queryParams.group_device_id = groupDeviceId
            }
            
            const response = await axios.get(`${this.baseUrl}/ports`, { params: queryParams })
            console.log('✅ [TabManager] 获取端口列表成功', { count: response.data?.length || 0 })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取端口列表失败:', error)
            throw error
        }
    }
    
    async getPortById(id) {
        console.log('🔍 [TabManager] 获取端口详情', { id })
        try {
            const response = await axios.get(`${this.baseUrl}/ports/${id}`)
            console.log('✅ [TabManager] 获取端口详情成功', { id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取端口详情失败:', error)
            throw error
        }
    }
    
    async createPort(data) {
        console.log('➕ [TabManager] 创建端口', { data })
        try {
            const response = await axios.post(`${this.baseUrl}/ports`, data)
            console.log('✅ [TabManager] 创建端口成功', { id: response.data?.id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 创建端口失败:', error)
            throw error
        }
    }
    
    async updatePort(id, data) {
        console.log('✏️ [TabManager] 更新端口', { id, data })
        try {
            const response = await axios.put(`${this.baseUrl}/ports/${id}`, data)
            console.log('✅ [TabManager] 更新端口成功', { id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 更新端口失败:', error)
            throw error
        }
    }
    
    async deletePort(id) {
        console.log('🗑️ [TabManager] 删除端口', { id })
        try {
            const response = await axios.delete(`${this.baseUrl}/ports/${id}`)
            console.log('✅ [TabManager] 删除端口成功', { id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 删除端口失败:', error)
            throw error
        }
    }
    
    async batchDeletePorts(ids) {
        console.log('🗑️ [TabManager] 批量删除端口', { ids })
        try {
            const response = await axios.delete(`${this.baseUrl}/ports/batch`, { data: { ids } })
            console.log('✅ [TabManager] 批量删除端口成功', { count: ids.length })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 批量删除端口失败:', error)
            throw error
        }
    }

    // 设备管理
    async getDeviceList(portId = null, params = {}) {
        console.log('🔍 [TabManager] 获取设备列表', { portId, params })
        try {
            // 构建查询参数
            const queryParams = { ...params }
            if (portId) {
                queryParams.port_id = portId
            }
            
            const response = await axios.get(`${this.baseUrl}/devices`, { params: queryParams })
            console.log('✅ [TabManager] 获取设备列表成功', { count: response.data?.length || 0 })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取设备列表失败:', error)
            throw error
        }
    }
    
    async getDeviceByPort(portId, params = {}) {
        console.log('🔍 [TabManager] 根据端口获取设备', { portId, params })
        try {
            const response = await axios.get(`${this.baseUrl}/ports/${portId}/devices`, { params })
            console.log('✅ [TabManager] 根据端口获取设备成功', { portId, count: response.data?.length || 0 })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 根据端口获取设备失败:', error)
            throw error
        }
    }
    
    async getDeviceById(id) {
        console.log('🔍 [TabManager] 获取设备详情', { id })
        try {
            const response = await axios.get(`${this.baseUrl}/devices/${id}`)
            console.log('✅ [TabManager] 获取设备详情成功', { id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取设备详情失败:', error)
            throw error
        }
    }
    
    async createDevice(data) {
        console.log('➕ [TabManager] 创建设备', { data })
        try {
            const response = await axios.post(`${this.baseUrl}/devices`, data)
            console.log('✅ [TabManager] 创建设备成功', { id: response.data?.id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 创建设备失败:', error)
            throw error
        }
    }
    
    async updateDevice(id, data) {
        console.log('✏️ [TabManager] 更新设备', { id, data })
        try {
            const response = await axios.put(`${this.baseUrl}/devices/${id}`, data)
            console.log('✅ [TabManager] 更新设备成功', { id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 更新设备失败:', error)
            throw error
        }
    }
    
    async deleteDevice(id) {
        console.log('🗑️ [TabManager] 删除设备', { id })
        try {
            const response = await axios.delete(`${this.baseUrl}/devices/${id}`)
            console.log('✅ [TabManager] 删除设备成功', { id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 删除设备失败:', error)
            throw error
        }
    }
    
    async batchDeleteDevices(ids) {
        console.log('🗑️ [TabManager] 批量删除设备', { ids })
        try {
            const response = await axios.delete(`${this.baseUrl}/devices/batch`, { data: { ids } })
            console.log('✅ [TabManager] 批量删除设备成功', { count: ids.length })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 批量删除设备失败:', error)
            throw error
        }
    }
    
    async checkDeviceStatus(id) {
        console.log('🔍 [TabManager] 检查设备状态', { id })
        try {
            const response = await axios.get(`${this.baseUrl}/devices/${id}/status`)
            console.log('✅ [TabManager] 检查设备状态成功', { id, status: response.data?.status })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 检查设备状态失败:', error)
            throw error
        }
    }

    // 变量管理
    async getVariableList(deviceId = null, params = {}) {
        console.log('🔍 [TabManager] 获取变量列表', { deviceId, params })
        try {
            // 构建查询参数
            const queryParams = { ...params }
            if (deviceId) {
                queryParams.device_id = deviceId
            }
            
            const response = await axios.get(`${this.baseUrl}/variables`, { params: queryParams })
            console.log('✅ [TabManager] 获取变量列表成功', { count: response.data?.length || 0 })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取变量列表失败:', error)
            throw error
        }
    }
    
    async getVariableByDevice(deviceId, params = {}) {
        console.log('🔍 [TabManager] 根据设备获取变量', { deviceId, params })
        try {
            const response = await axios.get(`${this.baseUrl}/devices/${deviceId}/variables`, { params })
            console.log('✅ [TabManager] 根据设备获取变量成功', { deviceId, count: response.data?.length || 0 })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 根据设备获取变量失败:', error)
            throw error
        }
    }
    
    async getVariableById(id) {
        console.log('🔍 [TabManager] 获取变量详情', { id })
        try {
            const response = await axios.get(`${this.baseUrl}/variables/${id}`)
            console.log('✅ [TabManager] 获取变量详情成功', { id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取变量详情失败:', error)
            throw error
        }
    }
    
    async createVariable(data) {
        console.log('➕ [TabManager] 创建变量', { data })
        try {
            const response = await axios.post(`${this.baseUrl}/variables`, data)
            console.log('✅ [TabManager] 创建变量成功', response)
            return response
        } catch (error) {
            console.error('❌ [TabManager] 创建变量失败:', error)
            throw error
        }
    }

    async createBatchVariable(data) {
        console.log('➕ [TabManager] 批量创建变量', { data })
        try {
            const response = await axios.post(`${this.baseUrl}/variables/batch`, data)
            console.log('✅ [TabManager] 批量创建变量成功', response)
            return response
        } catch (error) {
            console.error('❌ [TabManager] 批量创建变量失败:', error)
            throw error
        }
    }
    
    async updateVariable(id, data) {
        console.log('✏️ [TabManager] 更新变量', { id, data })
        try {
            const response = await axios.put(`${this.baseUrl}/variables/${id}`, data)
            console.log('✅ [TabManager] 更新变量成功', { id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 更新变量失败:', error)
            throw error
        }
    }
    
    async deleteVariable(id) {
        console.log('🗑️ [TabManager] 删除变量', { id })
        try {
            const response = await axios.delete(`${this.baseUrl}/variables/${id}`)
            console.log('✅ [TabManager] 删除变量成功', { id })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 删除变量失败:', error)
            throw error
        }
    }
    
    async batchDeleteVariables(ids) {
        console.log('🗑️ [TabManager] 批量删除变量', { ids })
        try {
            const response = await axios.delete(`${this.baseUrl}/variables/batch`, { data: { ids } })
            console.log('✅ [TabManager] 批量删除变量成功', { count: ids.length })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 批量删除变量失败:', error)
            throw error
        }
    }
    
    async getVariableValue(id) {
        console.log('🔍 [TabManager] 获取变量值', { id })
        try {
            const response = await axios.get(`${this.baseUrl}/variables/${id}/value`)
            console.log('✅ [TabManager] 获取变量值成功', { id, value: response.data?.value })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取变量值失败:', error)
            throw error
        }
    }
    
    async getVariableHistory(id, params = {}) {
        console.log('🔍 [TabManager] 获取变量历史数据', { id, params })
        try {
            const response = await axios.get(`${this.baseUrl}/variables/${id}/history`, { params })
            console.log('✅ [TabManager] 获取变量历史数据成功', { id, count: response.data?.length || 0 })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取变量历史数据失败:', error)
            throw error
        }
    }

    // 监控数据管理
    async getRealTimeData(params = {}) {
        console.log('🔍 [TabManager] 获取实时数据', { params })
        try {
            const response = await axios.get(`${this.baseUrl}/data/realtime`, { params })
            console.log('✅ [TabManager] 获取实时数据成功', { count: response.data?.length || 0 })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取实时数据失败:', error)
            throw error
        }
    }
    
    async getHistoryData(params = {}) {
        console.log('🔍 [TabManager] 获取历史数据', { params })
        try {
            const response = await axios.get(`${this.baseUrl}/data/history`, { params })
            console.log('✅ [TabManager] 获取历史数据成功', { count: response.data?.length || 0 })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取历史数据失败:', error)
            throw error
        }
    }
    
    async getStatistics(params = {}) {
        console.log('🔍 [TabManager] 获取统计数据', { params })
        try {
            const response = await axios.get(`${this.baseUrl}/data/statistics`, { params })
            console.log('✅ [TabManager] 获取统计数据成功', { stats: response.data })
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取统计数据失败:', error)
            throw error
        }
    }
    
    async exportData(params = {}) {
        console.log('📤 [TabManager] 导出数据', { params })
        try {
            const response = await axios.get(`${this.baseUrl}/data/export`, { 
                params,
                responseType: 'blob'
            })
            console.log('✅ [TabManager] 导出数据成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 导出数据失败:', error)
            throw error
        }
    }
}

export default MonitorTabManager 